//
//  ClubPushForumView.swift
//  77Club
//
//  Created by Apple on 2025/1/2.
//

import UIKit

class ClubPushForumTopicView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, RaeNetworkProtocol {

    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    var models: [ClubDynamicGameModel]?{
        didSet{
            collectionView.reloadData()
        }
    }
    
    /// 话题主页
    var selTopicBlock: ((_ model: ClubDynamicGameModel) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#F7F8FA".toColor
        
        loadData()
        subviews{
            collectionView
        }
        
        collectionView.leading(0).trailing(0).height(30.0.Scale).centerVertically()
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 8.0.Scale
        layout.minimumInteritemSpacing = 8.0.Scale
        layout.scrollDirection = .horizontal
//        layout.sectionInset = UIEdgeInsets(top: 0, left: 12.0.Scale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(ClubPushForumTopicItemCell.self, forCellWithReuseIdentifier: ClubPushForumTopicItemCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    private func loadData(){
        
        Task {
            let models = await network.requestSpecialData(.dynamicTopic(keyword: ""),type: ClubDynamicGameModel.self)
            if models.isSuccess, let models = models.models {
                var tempModels = [ClubDynamicGameModel]()
                for var md in models {
                    md.type = "1"
                    tempModels.append(md)
                }
                self.models = tempModels
                self.collectionView.reloadData()
            }
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return models?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ClubPushForumTopicItemCell.description(), for: indexPath) as? ClubPushForumTopicItemCell else { return UICollectionViewCell() }
        cell.model = models?[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if let md = models?[indexPath.row] {
            selTopicBlock?(md)
        }        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let name = "# " + (models?[indexPath.row].name ?? "")
        let font = 12.font_medium
        let attributedText = NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: font as Any])
        
        // 计算文本宽度
        let size = attributedText.boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, context: nil).size
        let width = size.width + 20.0.Scale
        
        return CGSize(width: width, height: 24.0.Scale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 话题分类cell
class ClubPushForumTopicItemCell: UICollectionViewCell {
    
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(12.font_medium).bgColor(.white)
        .alignment(.center).corner(12.0.Scale).end()
    
    var model: ClubDynamicGameModel?{
        didSet{
            guard let md = model else { return }
            nameLabel.text = "# " + md.name
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            nameLabel
        }

        nameLabel.fillContainer()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: 是否报错到草稿箱弹窗
class ClubPushForumDraftAlertView: UIView, RaeAnimationProtocol, RaeAnimationDelegate {
    
    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth - 64.0.Scale, height: 180.0.Scale)
    }
    
    func animationContainerViewStyle() -> RaeAnimationViewStyle {
        .center
    }
    
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#323233").font(16.font_semiBold)
        .alignment(.center).text("保存到草稿").end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#7D7E80").font(14.font_regular)
        .text("后续可在【我的-草稿箱】中继续编辑哦~")
        .alignment(.center).end()
    private let closeButton = c_make_ex(UIButton.self)
        .image("Forum_push_close").end()
    private let delButton = c_make_ex(UIButton.self)
        .image("Forum_push_discard").end()
    private let saveButton = c_make_ex(UIButton.self)
        .image("Forum_push_save").end()
    
    var discardBlock: (() -> Void)?
    var saveBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white
        rae.corner(12.0.Scale)
        
        subviews{
            titleLabel
            subLabel
            closeButton
            delButton
            saveButton
        }
        
        closeButton.trailing(16.0.Scale).top(24.0.Scale).size(24.0.Scale)
        titleLabel.top(45.0.Scale).centerHorizontally()
        subLabel.top(80.0.Scale).centerHorizontally()
        delButton.leading(16.0.Scale).width(120.0.Scale).height(40.0.Scale).bottom(24.0.Scale)
        saveButton.trailing(16.0.Scale).width(120.0.Scale).height(40.0.Scale).bottom(24.0.Scale)
        
        closeButton.addTarget(self, action: #selector(clickCloseButton), for: .touchUpInside)
        delButton.addTarget(self, action: #selector(clickDiscardButton), for: .touchUpInside)
        saveButton.addTarget(self, action: #selector(clickSaveButton), for: .touchUpInside)
    }
    
    @objc private func clickCloseButton(){
        entryAnimate.animation(dismiss: self)
    }
    
    @objc private func clickDiscardButton(){
        entryAnimate.animation(dismiss: self){
            self.discardBlock?()
        }
    }
    
    @objc private func clickSaveButton(){
        entryAnimate.animation(dismiss: self){
            self.saveBlock?()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK: 删除草稿箱弹窗
class ClubPushForumDraftDelView: UIView, RaeAnimationProtocol, RaeAnimationDelegate {
    
    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 315.0.Scale)
    }
    
    func animationContainerViewStyle() -> RaeAnimationViewStyle {
        .bottom
    }
    
    private let topView = c_make_ex(UIView.self)
        .bgColor(.white).corner(8).end()
    private let logoImageV = c_make_ex(UIImageView.self)
        .image("Forum_draft_logo").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#323233").font(15.font_semiBold)
        .text("确认删除吗?").end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#4B4C4D").font(14.font_medium)
        .text("删除后无法再次恢复, 请确认操作.").end()
    private let delButton = c_make_ex(UIButton.self)
        .bgColor(.white).corner(8).text("删除")
        .color("#B21212").font(15.font_semiBold).end()
    private let cancelButton = c_make_ex(UIButton.self)
        .bgColor(.white).corner(8).text("我手滑了")
        .color("#323233").font(15.font_semiBold).end()
    
    var delBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#EBEBEB".toColor
        rae.corner(12.0.Scale, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        subviews{
            topView.subviews{
                logoImageV
                titleLabel
                subLabel
            }
            delButton
            cancelButton
        }
        
        topView.trailing(16.0.Scale).top(26.0.Scale).leading(16.0.Scale).height(130.0.Scale)
        logoImageV.leading(24.0.Scale).top(24.0.Scale).width(295.0.Scale).height(24.0.Scale)
        titleLabel.leading(24.0.Scale).top(59.0.Scale)
        subLabel.leading(24.0.Scale).top(84.0.Scale)
        delButton.leading(16.0.Scale).trailing(16.0.Scale).height(45.0.Scale)
        delButton.Top == topView.Bottom + 12.0.Scale
        cancelButton.leading(16.0.Scale).trailing(16.0.Scale).height(45.0.Scale)
        cancelButton.Top == delButton.Bottom + 12.0.Scale
        
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
        delButton.addTarget(self, action: #selector(clickDelButton), for: .touchUpInside)
    }
    
    @objc private func clickCancelButton(){
        entryAnimate.animation(dismiss: self)
    }
    
    @objc private func clickDelButton(){
        entryAnimate.animation(dismiss: self){
            self.delBlock?()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
